package peace.gui.controls.box
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Stage;
import flash.filters.BitmapFilter;
import flash.filters.BitmapFilterQuality;
import flash.filters.BlurFilter;
import flash.filters.DropShadowFilter;
import flash.text.TextField;

import peace.gui.controls.buttons.PushButton;

/**
 * 
 * Панель с модальностью
 * 
 * @author peace
 * 
 */
public class Window extends Panel
{
	/**
	 * 
	 * Фильтр размытия модального фона
	 * 
	 */
	protected static var MODALITY_FILTER:BlurFilter = new BlurFilter(2,2,BitmapFilterQuality.HIGH);
	/**
	 * 
	 */	
	protected var modalityTarget:DisplayObjectContainer;
	/**
	 * 
	 */	
	protected var modalityFilter:BlurFilter;
	/**
	 * 
	 * @param content
	 * @param background
	 * @param titleLabel
	 * @param closeButton
	 * @param titleBackground
	 * 
	 */
	public function Window(content:DisplayObject,
							background:DisplayObject = null,
							titleLabel:TextField = null,
							closeButton:PushButton = null,
							titleBackground:DisplayObject = null
							)
	{
		super(content, background, titleLabel, closeButton, titleBackground);
	}
	/**
	 * 
	 * @param parent
	 * @param modalityTarget если модальное, то относительно кого
	 * @param alignCenter выравнивать ли по центру родителя
	 * 
	 */	
	public function open (parent:DisplayObjectContainer, modalityTarget:DisplayObjectContainer = null, alignCenter:Boolean = true):void
	{
		parent.addChild(this);
		
		if (alignCenter)
		{
			var w:Number = parent is Stage ? Stage(parent).stageWidth : parent.width;
			var h:Number = parent is Stage ? Stage(parent).stageHeight : parent.height;
			x = int((w - width)/2);
			y = int((h - height)/2);
		}
		
		if (modalityTarget)
		{
			modalityFilter = MODALITY_FILTER.clone() as BlurFilter;
			setModality(modalityTarget);
		}
	}
	/**
	 * 
	 * 
	 */	
	override public function close ():void
	{
		if (parent)	parent.removeChild(this);
		
		resetModality();
		
		super.close();
	}
	
	
	/**
	 * Делаем модальным относительно цели
	 *  
	 * @param target
	 * 
	 */	
	public function setModality (target:DisplayObjectContainer):void
	{
		modalityTarget = target;
		
		modalityTarget.mouseEnabled = modalityTarget.mouseChildren = false;
		
		var f:Array = modalityTarget.filters;
		f.push (modalityFilter);
		modalityTarget.filters = f;
	}
	/**
	 * 
	 * 
	 */	
	public function resetModality ():void
	{
		if (modalityTarget)
		{
			modalityTarget.mouseEnabled = modalityTarget.mouseChildren = true;
			
			var f:Array = modalityTarget.filters;
			var filter:BitmapFilter;
			f.splice(f.indexOf(modalityFilter), 1);
			modalityTarget.filters = f;
			
			modalityTarget = null;
		}
	}
	/**
	 * 
	 * 
	 */	
	override protected function dropShade():void
	{
		filters = [new DropShadowFilter(3,90,0,0.4,5,5,1,BitmapFilterQuality.HIGH)];													
	}
	
}
}